package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页大小
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> checkItems = checkItemDao.queryByPage(queryString);
        return new PageResult(checkItems.getTotal(), checkItems.getResult());
    }

    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.insert(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.queryById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.updateById(checkItem);
    }

    @Override
    public List<CheckItem> list() {
        return checkItemDao.findAll();
    }

    @Override
    public boolean removeById(Integer id) {
        int count = checkItemDao.isExist(id);
        if (count > 0) {
            return false;
        }
        checkItemDao.deleteById(id);
        return true;
    }
}
